reading-notes

Python Scope & the LEGB Rule:

Understanding Scope

Global scope: The names that you define in this scope are available to all your code.

Local scope: The names that you define in this scope are only available or visible to the code within the scope.

Python Scope vs Namespace

builtins: The Built-In Scope

Modifying the Behavior of a Python Scope

  1. globals()
  2. locals()
  3. dir()
  4. vars()

Conclusion

  1. Take advantage of Python scope to avoid or minimize bugs related to name collision
  2. Make good use of global and local names across your programs to improve code maintainability
  3. Use a coherent strategy to access, modify, or update names across all your Python code